package camp.tags;

import campskeleton.CampSkeleton;
import campskeleton.Plan;
import campskeleton.Profile;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import javax.swing.DefaultComboBoxModel;
import utils.WeakListener;
import utils.WeakListener.Changed;

/**
 *
 * @author twak
 */
public class JStringProfileCheckItem extends javax.swing.JPanel
{

    Plan plan;
    String profile;
    SubdivideTag.ProfileMerge toUpdate;
    Object selected; // tmp object transferred between
    WeakListener.Changed pListListener = new Changed()
    {

        @Override
        public void changed()
        {
            setupCombo();
        }
    };

    /** Creates new form JProfileProfileItem */
    public JStringProfileCheckItem()
    {
        initComponents();
    }

    public JStringProfileCheckItem( Plan plan, String profile, SubdivideTag.ProfileMerge toUpdate )
    {
        this.plan = plan;
        this.profile = profile;
        this.toUpdate = toUpdate;

        initComponents();

        CampSkeleton.instance.profileListChangedListeners.add( pListListener );

        inLabel.setText( profile.toString() );
        mergeCheckBox.setSelected( toUpdate.merge );

        outCombo.addMouseListener( new MouseAdapter()
        {

            @Override
            public void mouseClicked( MouseEvent e )
            {
                if ( e.getButton() == 3 )
                {
                    Object o = outCombo.getSelectedItem();
                    if ( o instanceof ProfileComboItem )
                        CampSkeleton.instance.setProfile( ((ProfileComboItem) o).p );
                }
            }
        } );

        setupCombo();
    }

    void setupCombo()
    {
        List<Profile> iles = plan.findProfiles();

        DefaultComboBoxModel cm = new DefaultComboBoxModel();

        cm.addElement( new ComboItem()
        {

            @Override
            public String toString()
            {
                return "X";
            }

            @Override
            public void onSelect()
            {
                toUpdate.profile = null;
                CampSkeleton.instance.somethingChanged();
            }
        } );

        for ( final Profile p : plan.findProfiles() )
        {
            Object o;
            cm.addElement( o = new ProfileComboItem( p ) );
            if ( p == toUpdate.profile )
                cm.setSelectedItem( o );
        }

        cm.addElement( new ComboItem()
        {

            @Override
            public String toString()
            {
                return "new";
            }

            @Override
            public void onSelect()
            {
                toUpdate.profile = plan.createNewProfile( null );
                CampSkeleton.instance.somethingChanged();
                CampSkeleton.instance.profileListChanged();
                setupCombo();
            }
        } );

        outCombo.setModel( cm );
    }

    static abstract class ComboItem
    {

        public abstract String toString();

        public abstract void onSelect();
    }

    class ProfileComboItem extends ComboItem
    {

        Profile p;

        public ProfileComboItem( Profile p )
        {
            this.p = p;
        }

        @Override
        public String toString()
        {
            return p.toString();
        }

        @Override
        public void onSelect()
        {
            toUpdate.profile = p;
            CampSkeleton.instance.somethingChanged();
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jPanel1 = new javax.swing.JPanel();
        inLabel = new javax.swing.JLabel();
        outCombo = new javax.swing.JComboBox();
        mergeCheckBox = new javax.swing.JCheckBox();

        setLayout(new java.awt.BorderLayout());

        jPanel1.setLayout(new javax.swing.BoxLayout(jPanel1, javax.swing.BoxLayout.LINE_AXIS));

        inLabel.setText("jLabel1");
        inLabel.setPreferredSize(new java.awt.Dimension(60, 15));
        jPanel1.add(inLabel);

        outCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        outCombo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                outComboActionPerformed(evt);
            }
        });
        jPanel1.add(outCombo);

        add(jPanel1, java.awt.BorderLayout.CENTER);

        mergeCheckBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mergeCheckBoxActionPerformed(evt);
            }
        });
        add(mergeCheckBox, java.awt.BorderLayout.EAST);
    }// </editor-fold>//GEN-END:initComponents

    private void outComboActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_outComboActionPerformed
        ComboItem o = (ComboItem) outCombo.getSelectedItem();
        o.onSelect();
    }//GEN-LAST:event_outComboActionPerformed

    private void mergeCheckBoxActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_mergeCheckBoxActionPerformed
    {//GEN-HEADEREND:event_mergeCheckBoxActionPerformed
        toUpdate.merge = mergeCheckBox.isSelected();
        CampSkeleton.instance.somethingChanged();
    }//GEN-LAST:event_mergeCheckBoxActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel inLabel;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JCheckBox mergeCheckBox;
    private javax.swing.JComboBox outCombo;
    // End of variables declaration//GEN-END:variables
}
